package com.sky.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @ClassName RedisConfiguration
 * @Author afy
 * @Date 2025年03月29日 20:12
 * @Version 1.0
 */
@Configuration
@Slf4j
public class RedisConfiguration {

    /*@Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
        log.info("开始创建redis模板对象...");
        RedisTemplate redisTemplate = new RedisTemplate();
        //设置redis的连接工厂对象
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //设置redis key的序列化器
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        return redisTemplate;
    }*/

    /**
     * 配置RedisTemplate以定义键值对存储方式
     * @param factory Redis连接工厂，用于创建和管理Redis连接
     * @return 配置好的RedisTemplate实例，用于与Redis进行交互
     */
    /*@Bean
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        log.info("开始创建redis模板对象...");
        // 创建RedisTemplate实例
        //RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        RedisTemplate template = new RedisTemplate();
        // 设置连接工厂
        template.setConnectionFactory(factory);

        // 序列化配置
        // 使用Jackson2JsonRedisSerializer来序列化和反序列化Object类型的值
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        // 创建ObjectMapper实例并配置其属性
        ObjectMapper om = new ObjectMapper();
        // 设置所有属性的可见性，以便于序列化和反序列化
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // 启用默认类型检测，以支持多态序列化和反序列化
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        // 设置ObjectMapper到Jackson2JsonRedisSerializer
        jackson2JsonRedisSerializer.setObjectMapper(om);

        // 使用StringRedisSerializer来序列化和反序列化String类型的键
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);

        // 初始化RedisTemplate
        template.afterPropertiesSet();

        // 返回配置好的RedisTemplate实例
        return template;
    }*/

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory,
                                                       Jackson2JsonRedisSerializer<Object> redisSerializer) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(redisSerializer); // 设置自定义的序列化器
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(redisSerializer);
        return template;
    }
}
